package jxc.servlet.inport;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import eu.log.LogFactory;
import eu.log.Logger;

import jxc.base.helper.IdGenerator;
import jxc.base.helper.TimeGenerator;
import jxc.base.helper.UserAuthenticator;
import jxc.data.hibernate.dao.GoodsDAO;
import jxc.data.hibernate.dao.OutportDAO;
import jxc.data.hibernate.dao.ProviderDAO;
import jxc.data.hibernate.pojo.Goods;
import jxc.data.hibernate.pojo.Outport;
import jxc.data.hibernate.pojo.Provider;

/**
 * 进货退货
 */
@WebServlet("/inport/return")
public class Return extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Logger logger=LogFactory.getLogger("销售");
	
	/**
	 * 返回更新界面
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProviderDAO pDao=new ProviderDAO();
		request.setAttribute("pList", pDao.getAll());
		request.getRequestDispatcher("/inport/return.jsp").forward(request, response);
	}

	/**
	 * 进货
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String provider=request.getParameter("provider");
		String payType=request.getParameter("pay_type");
		String goodsList=request.getParameter("goods");
		List<Outport> outports=resolveGoods(goodsList,provider,payType,UserAuthenticator.getUser(request).getName());
		//写入
		OutportDAO dao=new OutportDAO();
		double total=0;
		for(Outport outport:outports){
			dao.save(outport);
			total+=outport.getNumber()*outport.getPrice();
		}
		dao.commit();
		request.setAttribute("outports", outports);
		request.setAttribute("total", total);
		request.setAttribute("time", TimeGenerator.getTimeString());
		doGet(request, response);
	}

	/**
	 * 解析页面商品列表，返回销售列表
	 */
	protected List<Outport> resolveGoods(String goodsList,String provider,String payType,String username) throws IOException{
		List<Outport> outports=new ArrayList<Outport>();
		Goods good=null;
		Outport outport=null;
		GoodsDAO gDao=new GoodsDAO();
		String id=null;
		String amount=null;
		String price=null;
		String[] goods=goodsList.split(";");
		String[] fields=null;
		for(String line:goods){
			fields=line.split("#");
			id=fields[0];
			amount=fields[1];
			price=fields[2];
			good=gDao.getById(id);
			if(good==null)
				continue;
			outport=new Outport();
			outport.setId(IdGenerator.getSaleId());
			outport.setNumber(Integer.parseInt(amount));
			outport.setOperateperson(username);
			outport.setPaytype(payType);
			outport.setPrice(Float.parseFloat(price));
			outport.setOutporttime(new Date());
			outport.setTbGoods(good);
			outport.setTbProvider(getProvider(provider));
			outports.add(outport);
		}
		return outports;
	}
	
	/**
	 * 查找供货商
	 */
	protected Provider getProvider(String id){
		ProviderDAO dao=new ProviderDAO();
		return dao.getById(id);
	}
}
